import uuid
import json
from datetime import datetime

class MessageTask:
    def __init__(self, title, message_type, content, webhook_url, task_id=None, created_at=None, updated_at=None):
        self.task_id = task_id or str(uuid.uuid4())
        self.title = title
        self.message_type = message_type  # 'text' or 'news'
        self.content = content  # 对于text是文本内容，对于news是JSON格式的图文内容
        self.webhook_url = webhook_url
        self.created_at = created_at or datetime.now().isoformat()
        self.updated_at = updated_at or datetime.now().isoformat()

    def to_dict(self):
        return {
            'task_id': self.task_id,
            'title': self.title,
            'message_type': self.message_type,
            'content': self.content,
            'webhook_url': self.webhook_url,
            'created_at': self.created_at,
            'updated_at': self.updated_at
        }

    @classmethod
    def from_dict(cls, data):
        return cls(
            title=data['title'],
            message_type=data['message_type'],
            content=data['content'],
            webhook_url=data['webhook_url'],
            task_id=data['task_id'],
            created_at=data['created_at'],
            updated_at=data['updated_at']
        )

    def update(self, title=None, message_type=None, content=None, webhook_url=None):
        if title is not None:
            self.title = title
        if message_type is not None:
            self.message_type = message_type
        if content is not None:
            self.content = content
        if webhook_url is not None:
            self.webhook_url = webhook_url
        self.updated_at = datetime.now().isoformat()